$(document).ready(function (){
	view_init();
	event_init();
});

var url_param_id = getQueryString("id");

function view_init()
{
	loadingStart();
	$("[data-type=dateAll]").each(function (){
		$(this).datetimepicker({
			  timeFormat: "HH:mm:ss",
	          dateFormat: "yy-mm-dd",
			  monthNames: [ "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" ]
		});
	});
	
	// 初始化新闻分类
	um_ajax_post(
			"/article/catalog/select.do",
			null,
			function (data){
				if (data && data.catalogs)
				{
					for (var i=0;i<data.catalogs.length;i++)
					{
						$("[data-id=catalog]")
							.append('<option value="'+data.catalogs[i].catalogID+'">'
										+data.catalogs[i].catalogName+'</option>');
					}
				}
			},
			null,false);
	
	UEDITOR_CONFIG.UEDITOR_HOME_URL = '/ueditor/';
 	UE.getEditor('content');
 	
 	if (url_param_id)
 	{
 		um_ajax_post(
 				"/article/select.do",
 				{"articleId":url_param_id},
 				function (data){
 					if (data && data.articles)
 					{
 	 					$("#main-content").umDataBind("render" ,data.articles[0] ,"");
 	 					UE.getEditor('content').addListener("ready", function () {
 	 						UE.getEditor('content').setContent(data.articles[0].content);
 	 					});
 	 					
 	 					$("#previewPicture").next().show();
 	 					$("#previewPicture").next().attr("src" ,data.articles[0].previewPicture);
 	 					if (data.articles[0].isHomePage == 1)
 	 					{
 	 						$("#homePageDiv").show();
 	 						$("#homePageChk").prop("checked" ,"checked");
 	 						$("#homePageImage").next().show();
 	 						$("#homePageImage").next().attr("src" ,data.articles[0].homePageImage);
 	 					}
 	 					
 	 					if (data.articles[0].isHeadline == 1)
 	 					{
 	 						$("#headLineDiv").show();
 	 						$("#headLineChk").prop("checked" ,"checked");
 	 						$("#headlineImage").next().show();
 	 						$("#headlineImage").next().attr("src" ,data.articles[0].headlineImage);
 	 					}
 					}

 				},
 				null,false);
 	}
 	
	loadingEnd();
	$("#main-content").show();
}

function event_init()
{
	// tab页点击事件
	$("#menu_ul").find("li").click(function (){
		$("#menu_ul").find("li").attr("class" ,"");
		$(this).attr("class" ,"active");
		$("[class=panel-content]").hide();
		$("#" + $(this).attr("data-div")).show();
		$("#ueditor_0").show();
	});
	
	$("#homePageChk").click(function (){
		if ($(this).is(":checked"))
		{
			$("#homePageDiv").show();
		}
		else
		{
			$("#homePageDiv").hide();
		}
	});
	
	$("#headLineChk").click(function (){
		if ($(this).is(":checked"))
		{
			$("#headLineDiv").show();
		}
		else
		{
			$("#headLineDiv").hide();
		}
	});
	
	// 列表图片
    $("#previewPicture_img").dmUploader({
        url: INDEX_WEBAPP + '/article/image/upload.do',
        dataType: 'json',
        allowedTypes: 'image/*',
        extraData: {data:JsonTools.encode({articleId:getQueryString("id")})},
        onUploadSuccess: function(id, data){
        	index_show("上传成功");
        	$("#previewPicture").val(data.result_map.url);
        	$("#previewPicture").next().attr("src" ,data.result_map.url);
        	$("#previewPicture").next().show();
        }
    });
    
	// 轮播图片
    $("#homePageImage_img").dmUploader({
        url: INDEX_WEBAPP + '/article/image/upload.do',
        dataType: 'json',
        allowedTypes: 'image/*',
        extraData: {data:JsonTools.encode({articleId:getQueryString("id")})},
        onUploadSuccess: function(id, data){
        	index_show("上传成功");
        	$("#homePageImage").val(data.result_map.url);
        	$("#homePageImage").next().attr("src" ,data.result_map.url);
        	$("#homePageImage").next().show();
        }
    });
    
	// 头条图片
    $("#headlineImage_img").dmUploader({
        url: INDEX_WEBAPP + '/article/image/upload.do',
        dataType: 'json',
        allowedTypes: 'image/*',
        extraData: {data:JsonTools.encode({articleId:getQueryString("id")})},
        onUploadSuccess: function(id, data){
        	index_show("上传成功");
        	$("#headlineImage").val(data.result_map.url);
        	$("#headlineImage").next().attr("src" ,data.result_map.url);
        	$("#headlineImage").next().show();
        }
    });
    
    $("#edit").click(function (){
    	article_edit();
    });
    
	// 返回按钮
	$("#back").click(function (){
		window.location.href = "/article/direction/index.do";
	});
}

function article_edit()
{
	if (!$("#main-content").validate())
	{
		return false;
	}
	var expireTime = $("[data-id=expireTime]").val();
	if (moment(expireTime).isBefore(moment()))
	{
		$("body").umDialog("alert" ,{"content":"过期时间不能小于当前时间"});
		return false;
	}
	var obj = $("#main-content").umDataBind("serialize" ,"");
	obj.content = UE.getEditor('content').getContent();
	var url = "/article/add.do";

	if (url_param_id)
	{
		url = "/article/update.do";
		obj.articleId = url_param_id;
	}
	
	if ($("#homePageChk").is(":checked"))
	{
		obj.isHomePage = 1;
	}
	
	if ($("#headLineChk").is(":checked"))
	{
		obj.isHeadline = 1;
	}
	
	um_ajax_post(
			url,
			obj,
			function (data){
				window.location.href = "/article/direction/index.do?flag=" + SUCCESS;
			},
			null);
}